<template>
  <view class="yzm">
    <uni-forms ref="loginRef" v-model="state.formData" :rules="state.rules" validateTrigger="bind">
      <uni-forms-item name="mobile" label="手机号">
        <uni-easyinput placeholder="请输入手机号" v-model="state.formData.mobile" type="number" :inputBorder="false">

        </uni-easyinput>
      </uni-forms-item>

      <uni-forms-item name="code" label="验证码">
        <uni-easyinput placeholder="请输入验证码" v-model="state.formData.code" :inputBorder="false" type="number" maxlength="4">
          <template v-slot:right>
            <button class="ss-reset-button code-btn code-btn-start" :disabled="state.isMobileEnd"
              :class="{ 'code-btn-end': state.isMobileEnd }" @tap="getSmsCode('smsLogin', state.formData.mobile)">
              {{ getSmsTimer('smsLogin') }}
            </button>
          </template>
        </uni-easyinput>
      </uni-forms-item>
      <view class="row">
        <view @click="register">注册账号</view>
        <view @tap="resetPassword">忘记密码</view>
      </view>
      <button class="submit" @tap="loginSubmit"> 立即登录 </button>
      <view class="line">
        <view>OR</view>
      </view>
    </uni-forms>
      
   
  </view>

</template>

<script setup>
import sheep from '@/sheep';
import { ref, reactive, unref } from 'vue';
import { code, mobile } from '@/sheep/validate/form';
import {  getSmsCode, getSmsTimer } from '@/sheep/hooks/useModal';
const emits = defineEmits(['onConfirm']);
const props = defineProps({
    agreeStatus: {
      type: Boolean,
      default: false,
    },
  });
const loginRef = ref(null);
// 数据
const state = reactive({
  isMobileEnd: false, // 手机号输入完毕
  formData: {
    mobile: '', // 手机号
    code: '', // 验证码
  },
  rules: {
    code,
    mobile,
  },
});



// 3.账号登录

async function loginSubmit() {
  const validate = await unref(loginRef)
    .validate()
    .catch((error) => {
    });
  if (!validate) return;
  if (!props.agreeStatus) {
      emits('onConfirm', true)
      sheep.$helper.toast('请勾选相关协议');
      return;
    }
  const { code } = await sheep.$api.user.smsLogin({
    mobile: state.formData.mobile,
    code: state.formData.code
  });
  if (code === 1) {
    uni.showToast({
      title: '登录成功',
      icon: "none"
    });
    sheep.$router.go('/pages/staff/index/index');
  }
}

//跳转注册
const register = () => {
  sheep.$router.go('/pages/public/auth/register');

}
const resetPassword = () => {
  sheep.$router.go('/pages/public/auth/reset_password');
  
}
</script>

<style lang="scss">
@import '../auth/components/login.scss';

#app {
  .form-box {
    justify-content: space-around;

  }
}


</style>